package com.fanfanwang.ffw.app.asynctask;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.fanfanwang.ffw.app.R;
import com.fanfanwang.ffw.app.bean.SucGetYuYueListBean;
import com.fanfanwang.ffw.app.bean.YuyueInfoBean;
import com.fanfanwang.ffw.app.common.Md5Util;
import com.fanfanwang.ffw.app.common.ToastAlone;
import com.fanfanwang.ffw.app.net.HttpRequest.HttpRequestException;
import com.fanfanwang.ffw.app.net.IssAsyncTask;
import com.fanfanwang.ffw.app.net.IssNetLib;
import com.fanfanwang.ffw.app.net.WebServiceHelper;
import com.fanfanwang.ffw.app.receiver.OneShotAlarm;
import com.fanfanwang.ffw.app.tools.SharedPreferencesUtil;
import com.fanfanwang.ffw.app.tools.TimeUtils;

public class GetYuyueListAsyncTask extends
		IssAsyncTask<String, String, SucGetYuYueListBean> {
	private Activity mActivity;
	private String userid;

	public GetYuyueListAsyncTask(Activity activity, String userid) {
		super(activity);
		mActivity = activity;
		this.userid = userid;
	}

	@Override
	protected SucGetYuYueListBean doInBackground(String... params) {
		// TODO addAuctionPrice($apiusername,$apipassword,$userid,$pid,$price)
		SucGetYuYueListBean bean = null;
		LinkedHashMap<String, Object> paramsMap = new LinkedHashMap<String, Object>();
		paramsMap.put("apiusername", "parking");
		paramsMap.put("apipassword", Md5Util.getMD5To32("parking"));
		paramsMap.put("userid", userid);
		try {
			bean = IssNetLib.getInstance(mActivity).getYuyueList(
					WebServiceHelper._GETYUYUELIST, paramsMap);
		} catch (HttpRequestException e) {
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_net_error);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			// exception = getResources().getString(R.string.e_json);
		}
		return bean;
	}

	@Override
	protected void onPostExecute(SucGetYuYueListBean result) {
		super.onPostExecute(result);
		if (exception != null) {
			ToastAlone.showToast(mActivity, exception);
			return;
		}
		if (result != null) {
			if (result.status == 1) {

				if (result.data != null) {
					// TODO 调用接口把值传过去。
					String remindtime = SharedPreferencesUtil.getInstance(
							mActivity).getRemindtime();
					int aheadtime = 20;
					if (remindtime.contains("15")) {
						aheadtime = 15;
					} else if (remindtime.contains("10")) {
						aheadtime = 10;
					}
					ArrayList<YuyueInfoBean> list = result.data;
					for (int i = 0; i < list.size(); i++) {
						String date = TimeUtils.parseLongToDate(
								list.get(i).orderstarttime,
								"yyyy-MM-dd HH:mm:ss");
					
						if (TimeUtils.CompareCurrGTOther(date, "", aheadtime)) {
							Intent intent = new Intent(mActivity,
									OneShotAlarm.class);
							PendingIntent sender = PendingIntent.getBroadcast(
									mActivity, 0, intent, 0);
							AlarmManager am = (AlarmManager) mActivity
									.getSystemService(Context.ALARM_SERVICE);
							//这里的第二参数表示希望什么时候闹钟通知，就是通知的时间，不过需要转化成long的形式（毫秒）
							am.set(AlarmManager.RTC_WAKEUP,
									TimeUtils.parseDateToMilliAheadSecond(date,aheadtime),
									sender);
						}
					}
				}
			} else if (result.status == 0) {
				if (result.msg != null && !TextUtils.isEmpty(result.msg)) {
//					try {
//						ToastAlone.showToast(mActivity,
//								URLDecoder.decode(result.msg, "utf-8"),
//								ToastAlone.LENGTH_SHORT);
//					} catch (UnsupportedEncodingException e) {
//						e.printStackTrace();
//					}
				} else {

//					ToastAlone.showToast(
//							mActivity,
//							mActivity.getResources().getString(
//									R.string.e_net_error)).show();

				}
			}
		}
	}
}
